package com.example.pagingdemo.paging.doctor;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pagingdemo.databinding.ItemDoctorBinding;
import com.example.pagingdemo.model.DoctorVo;

/**
 * @author: tuyrt7 on 2020/10/27.
 */
public class DoctorAdapter extends PagedListAdapter<DoctorVo.DocArrBean, DoctorAdapter.ViewHolder> {

    public DoctorAdapter() {
        super(DIFF_CALLBACK);
    }

    private static DiffUtil.ItemCallback<DoctorVo.DocArrBean> DIFF_CALLBACK = new DiffUtil.ItemCallback<DoctorVo.DocArrBean>() {
        @Override
        public boolean areItemsTheSame(@NonNull DoctorVo.DocArrBean oldItem, @NonNull DoctorVo.DocArrBean newItem) {
            return oldItem.doctor_id.equals(newItem.doctor_id);
        }

        @Override
        public boolean areContentsTheSame(@NonNull DoctorVo.DocArrBean oldItem, @NonNull DoctorVo.DocArrBean newItem) {
            return oldItem.doctor_id.equals(newItem.doctor_id) &&
                    oldItem.head_rul.equals(newItem.head_rul) &&
                    oldItem.name.equals(newItem.name)
                    ;
        }
    };

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDoctorBinding binding = ItemDoctorBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DoctorVo.DocArrBean item = getItem(position);
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ItemDoctorBinding mBinding;

        public ViewHolder(@NonNull ItemDoctorBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void bindData(DoctorVo.DocArrBean arrBean) {
            mBinding.setDocArrBean(arrBean);
        }
    }
}
